<template>
  <div class="layout_card">
    <el-card>
      <div class="table_section" style="margin: 10px 0">
        <el-table
          :data="tableData"
          border
          row-key="id"
          default-expand-all
          @selection-change="TABLE_SELECT_CALLBACK"
        >
          <el-table-column prop="name" label="名称" />
          <el-table-column prop="code" label="权限值" />
          <el-table-column label="操作" fixed="right">
            <template #default="{ row }">
              <el-button
                type="primary"
                icon="Plus"
                size="small"
                title="添加菜单"
                :disabled="row.level == 4"
                @click="OPEN_DIALOG('add', row)"
              >
                添加菜单
              </el-button>

              <el-button
                type="primary"
                icon="Edit"
                size="small"
                title="编辑"
                :disabled="row.level == 1"
                @click="OPEN_DIALOG('edit', row)"
              >
                编辑
              </el-button>

              <el-button
                type="danger"
                icon="Delete"
                size="small"
                title="删除"
                :disabled="row.level == 1"
                @click="DELETE_ATTR(row.id)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="500">
      <el-form
        :model="permissionData"
        ref="ruleFormRef"
        :rules="rules"
        label-width="80"
      >
        <el-form-item label="权限名称" prop="name">
          <el-input
            v-model="permissionData.name"
            placeholder="请输入权限名称"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="权限值" prop="code">
          <el-input
            v-model="permissionData.code"
            placeholder="请输入权限值"
            autocomplete="off"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="CLOSE_DIALOG">取消</el-button>
          <el-button type="primary" @click="ADDUPDATE_DIALOG">确认</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts">
export default {
  name: 'permissionLayout',
}
</script>
<script setup lang="ts">
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import { onMounted, onBeforeUnmount, ref, reactive } from 'vue'

import { reqAddOrUpdateTrademark, reqDeletTrademark } from '@/api/product'
import { resPermissionList } from '@/api/acl'
import type {} from '@/api/acl/type'
import { publicConfirm } from '@/utils/handleData'
onMounted(() => {
  GET_LIST()
})
onBeforeUnmount(() => {})
let tableData = ref<any>([])
let DELETE_IDS = ref<string>('')
const dialogVisible = ref(false)
let dialogTitle = ref('')
const ruleFormRef = ref<FormInstance>()
const permissionData_init = {
  id: '',
  code: '',
  name: '',
  logoUrl: '',
}

let permissionData = reactive({ ...permissionData_init })

const GET_LIST = async () => {
  let result: any = await resPermissionList()
  if (result.code == 200) {
    tableData.value = result.data
  }
}

// 删除SPU
const DELETE_ATTR = async (id = 0) => {
  const flag = await publicConfirm({
    title: '提示',
    content: '确认删除该菜单？',
  })
  let type = '删除'
  if (flag) {
    let result: any = await reqDeletTrademark(id)
    if (result.code == 200) {
      ElMessage({
        type: 'success',
        message: `${type}成功`,
      })
      GET_LIST()
    } else {
      ElMessage({
        type: 'error',
        message: `${type}失败`,
      })
    }
  } else {
    ElMessage({
      type: 'info',
      message: `取消${type}`,
    })
  }
}

const ADDUPDATE_DIALOG = async () => {
  if (!ruleFormRef.value) return
  await ruleFormRef.value.validate()
  let result: any = await reqAddOrUpdateTrademark(permissionData)
  let type = '添加'
  if (permissionData.id) {
    type = '修改'
  }
  if (result.code == 200) {
    ElMessage({
      type: 'success',
      message: `${type}成功`,
    })
  } else {
    ElMessage({
      type: 'error',
      message: `${type}成功`,
    })
  }
  CLOSE_DIALOG()
}
const OPEN_DIALOG = (type, row) => {
  //   console.log(id)
  INIT_DIALOG()
  if (type == 'edit') {
    //修改
    dialogTitle.value = '编辑菜单'
    Object.assign(permissionData, row)
  } else {
    // 添加
    dialogTitle.value = '添加菜单'
  }
  dialogVisible.value = true
}

const INIT_DIALOG = () => {
  Object.assign(permissionData, permissionData_init)
  if (!ruleFormRef.value) return
  ruleFormRef.value.clearValidate('roleName')
}
const CLOSE_DIALOG = () => {
  dialogVisible.value = false
}

const rules = reactive<FormRules<any>>({
  roleName: [{ required: true, message: '请输入菜单姓名', trigger: 'blur' }],
})
// let checkedRoles = ref<any>([])
const TABLE_SELECT_CALLBACK = (selects) => {
  DELETE_IDS.value = selects.map((item) => item.id).join(',')
}
</script>

<style scoped lang="scss">
.filter_section .el-input {
  --el-input-width: 220px;
}
.filter_section .el-input {
  --el-select-width: 220px;
}
</style>
